package com.chenglu.android.couponcenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseFullScreenActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.StatusBarUtil;
import com.chenglu.android.couponcenter.adapter.CenterTabAdapter;
import com.chenglu.android.couponcenter.adapter.CouponCenterAdapter;
import com.chenglu.android.couponcenter.mvp.contract.CouponCenterContract;
import com.chenglu.android.couponcenter.mvp.presenter.CouponCenterPresenter;
import com.chenglu.android.goodsinfo.bean.ShopCouponPage;
import com.chenglu.android.goodsinfo.infoutils.GoodsInfoUtils;
import com.chenglu.android.index.bean.TabTitleBean;
import com.chenglu.android.mine.bean.CommonDataBean;
import com.chenglu.android.mine.bean.CouponBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023/11/7 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：领劵中心
 */
public class CouponCenterActivity extends BaseFullScreenActivity<CouponCenterPresenter> implements CouponCenterContract.View {


    @BindView(R.id.ic_back)
    ImageView icBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.rv_order_title)
    RecyclerView rvOrderTitle;
    @BindView(R.id.rv_order_data)
    RecyclerView rvOrderData;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.ll_loading_data)
    LinearLayout llLoadingData;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_coupon_center);
        ButterKnife.bind(this);
        StatusBarUtil.immersive(this);
        StatusBarUtil.setPaddingSmart(this, toolbar);
        mPresenter = new CouponCenterPresenter();
        mPresenter.attachView(this);
        tvTitle.setText("领劵中心");
        tvTitle.setTextColor(mContext.getResources().getColor(R.color.all_black_text));
        initTabTitleAdapter();

        initTitleDataAdapter();

        initRefreshLayout();
        mPresenter.getTabTitle();

    }

    private CenterTabAdapter tabTitleAdapter;
    List<TabTitleBean.Data> tabTitleList = new ArrayList<>();

    //标题数据的列表
    private void initTabTitleAdapter() {
        tabTitleAdapter = new CenterTabAdapter(mContext, tabTitleList);
        rvOrderTitle.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.HORIZONTAL, false));

        rvOrderTitle.setAdapter(tabTitleAdapter);

        tabTitleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                tabTitleAdapter.setThisPosition(position);
                if (!couponList.isEmpty()) {
                    current = 1;
                    couponList.clear();
                }

                if(position==0){
                    tab0 = "0";
                    mPresenter.getShopCouponPage(getToken(), setTabTitleMap(false, tab0));
                }else {
                    tab0 = tabTitleAdapter.getData().get(position).getShopsTypeId();
                    mPresenter.getShopCouponPage(getToken(), setTabTitleMap(true, tab0));
                }

            }
        });
    }

    List<ShopCouponPage.Data.Records> couponList = new ArrayList<>();
    private CouponCenterAdapter couponCenterAdapter;

    private  int mCoupouPosition;
    //优惠卷的商品列表 刷新和加载
    private void initTitleDataAdapter() {

        couponCenterAdapter = new CouponCenterAdapter(mContext, couponList);
        LinearLayoutManager gridLayoutManager = new LinearLayoutManager(mContext);
        rvOrderData.setLayoutManager(gridLayoutManager);
        rvOrderData.setAdapter(couponCenterAdapter);

        couponCenterAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (couponCenterAdapter.getData().get(position).getShopsTypeId() == 2) {
//                    GoodsInfoUtils.goodsRestantInfo(mContext, couponCenterAdapter.getData().get(position).getId());
//                } else {
//                    GoodsInfoUtils.goodsInfo(mContext, couponCenterAdapter.getData().get(position).getId()
//                            , couponCenterAdapter.getData().get(position).getShopsTypeId());
//                }

            }
        });

        couponCenterAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                 mCoupouPosition = position;
                TextView textView = view.findViewById(R.id.tv_receive_now);
                if(textView.getText().toString().equals("去使用")){
                    GoodsInfoUtils.goodsStoreInfo(mContext,couponCenterAdapter.getData().get(position).getTenantId(),couponCenterAdapter.getData().get(position).getShopsTypeId());

                }else if(textView.getText().toString().equals("立即领取")){
//                    请求接口数据
                    mPresenter.getShopCouponreceive(getToken(),couponCenterAdapter.getData().get(position).getId());
                }
            }
        });

    }
    //刷新
    private void initRefreshLayout() {
        smartrefreshlayout.setEnableRefresh(false);
        smartrefreshlayout.setEnableLoadMore(true);
        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //id 需要传参过来
                current = 1;
                //需要清空数据再加
                couponList.clear();
                if(tab0.equals("0")){
                    mPresenter.getShopCouponPage(getToken(),setTabTitleMap(false, tab0));
                }else {
                    mPresenter.getShopCouponPage(getToken(),setTabTitleMap(true, tab0));
                }



            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                current += 1;
                if(tab0.equals("0")){
                    mPresenter.getShopCouponPage(getToken(),setTabTitleMap(false, tab0));
                }else {
                    mPresenter.getShopCouponPage(getToken(),setTabTitleMap(true, tab0));
                }



            }
        });
    }

    @Override
    public void getCouponListSuccess(CouponBean bean) {

    }
    boolean isAddFooterView = true;
    @Override
    public void getShopCouponPage(ShopCouponPage bean) {

        stopRefresh(smartrefreshlayout);
//        LogUtils.e(new Gson().toJson(bean.getData().getRecords()));
        if (bean.getData().getRecords().size() > 0) {

            llLoadingData.setVisibility(View.VISIBLE);
            llLoadingNoData.setVisibility(View.GONE);
            for (int i = 0; i < bean.getData().getRecords().size(); i++) {
                //添加集合中banner的数据
                couponList.add(bean.getData().getRecords().get(i));
            }
            LogUtils.w("全部 list" + couponList.size());
            if (bean.getData().getTotal() < Constant.SIZETEN) {
                smartrefreshlayout.setEnableRefresh(false);
            }
            if (current * Constant.SIZETEN >= bean.getData().getTotal()) {
                smartrefreshlayout.setEnableLoadMore(false);

                if (isAddFooterView) {
                    View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                    inflate.findViewById(R.id.tv_no_more).setBackgroundColor(getResources().getColor(R.color.color_f6f6f6));
                    couponCenterAdapter.addFooterView(inflate);
                    isAddFooterView = false;
                }


            } else {
                isAddFooterView = true;
                couponCenterAdapter.removeAllFooterView();
                smartrefreshlayout.setEnableLoadMore(true);

            }
            couponCenterAdapter.notifyDataSetChanged();
        } else {
            if (couponList.isEmpty()) {
                llLoadingData.setVisibility(View.GONE);
//                llLoadingNoData.setBackground(getResources().getDrawable(R.drawable.corners_f6f6f6_default));
                llLoadingNoData.setVisibility(View.VISIBLE);
            }

        }
    }

    @Override
    public void getShopCouponreceive(CommonDataBean bean) {

        couponCenterAdapter.getData().get(mCoupouPosition).setIsReceive(true);
        couponCenterAdapter.notifyDataSetChanged();
    }

    @Override
    public void getTabTitle(TabTitleBean bean) {

        if (bean.getCode().equals(Constant.HTTPCODE200)) {

            for (int i = 0; i < bean.getData().size(); i++) {
                tabTitleList.add(bean.getData().get(i));
            }
            tabTitleAdapter.notifyDataSetChanged();

            //先把标题获取后再请求精选数据
            mPresenter.getShopCouponPage(getToken(), setTabTitleMap(false, tab0));
        }

//         mPresenter.getShopCouponPage();
    }

    private int current = 1;
    private String tab0 = "0"; //精选

    private Map<String, String> setTabTitleMap(boolean istype, String shopsId) {
        Map<String, String> map = new HashMap<>();
        if (istype) {
            map.put("shopsTypeId", shopsId);
        }
        map.put("current", +current + "");
        map.put("size", Constant.SIZETEN + "");
        return map;
    }

    @OnClick({R.id.ic_back, R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ic_back:
                finish();
                break;
            case R.id.ll_back:
                finish();
                break;
        }
    }
}
